package com.winit.openapi.wh.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.inbound.InboundOrderFeeService;
import com.winit.oms.spi.inbound.command.OperateByKeyCommand;
import com.winit.oms.spi.inbound.vo.InboundOrderFeeResultVo;
import com.winit.oms.spi.inbound.vo.InboundOrderFeeVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单费用相关接口
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * bing.gu 	1.0  		2015年11月11日 	Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/fee")
public class InboundOrderFeeController extends BaseController {

    @Resource
    private InboundOrderFeeService inboundOrderFeeService;

    /**
     * 海外仓入库单费用预估
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/estimate")
    public String estimate(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        Object obj = requestMsg.getData();
        OperateByKeyCommand<String> command = new OperateByKeyCommand<String>();
        command.setCtx(CommandContext.getContext());
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.isNotNull(json, "orderNo");
            command.setKey(json.getString("orderNo"));
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        // 调用费用预估接口
        InboundOrderFeeResultVo result = inboundOrderFeeService.estimateCalculateFee(command);
        List<Map<String, Object>> chargesList = new ArrayList<Map<String, Object>>();
        Map<String, Object> resultMap = new HashMap<String, Object>();
        Map<String, Object> dataMap = new HashMap<String, Object>();
        for (InboundOrderFeeVo fee : result.getFeeList()) {
            dataMap = new HashMap<String, Object>();
            dataMap.put("feeName", fee.getName());
            dataMap.put("feeNode", fee.getNode());
            dataMap.put("fee", fee.getFee().setScale(2, BigDecimal.ROUND_HALF_UP)); // 保留2位小数
            dataMap.put("feeCurrency", fee.getCurrency());
            chargesList.add(dataMap);
        }
        resultMap.put("feeList", chargesList);
        resultMap.put("totalFeeCurrency", result.getTotalFeeCurrency());
        resultMap.put("totalFee", result.getTotalFee().setScale(2, BigDecimal.ROUND_HALF_UP));// 保留2位小数
        responseMsg.setData(resultMap);
        responseMsg.setMsg("SUCCESS");
        return SUCCESS;
    }

}
